/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.jm.financeiro.filter;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.entity.CentroCusto;
import br.jm.financeiro.sistema.eao.CentroCustoEao;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author joao
 */
public class CentroCustoConverter implements Converter {
    private final CentroCustoEao CentroCustoJpa;
    private CentroCusto selectedCentroCusto;
    
    public CentroCustoConverter() throws EaoException {
        CentroCustoJpa = CentroCustoEao.getInstance();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        int number = Integer.parseInt(value);
        try {
            selectedCentroCusto = CentroCustoJpa.getPorId(number);
        } catch (Exception ex) {
            selectedCentroCusto = null;
        }
        return selectedCentroCusto;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.toString().trim().equals("")) {
            return "";
        } else {
            return String.valueOf(((CentroCusto) value).getIdCusto());
        }
    }
    
}


